package com.cskaoyan.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author stone
 * @date 2022/07/14 10:04
 */
//@Component
public class CustomHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object handler, Exception exception) {
        // 构建一个业务场景：如果发生异常，就告诉用户：应用程序正在升级
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/exception.jsp");
        //想要对算术异常做特殊的处理 → 需要自己做异常类型的判断
        if (exception instanceof ArithmeticException) {
            modelAndView.addObject("msg", "算术异常");
        }
        return modelAndView;
    }
}
